package com.mcx.modules.proto;

import com.mcx.modules.common.IProto;
import com.mcx.modules.common.MMO_DataStream;

import java.io.*;
import java.util.*;

/**
 * 服务器返回角色充值信息
 */
public class RoleData_RechargeReturnProto implements IProto
{
    private static MMO_DataStream dataStream = new MMO_DataStream();

    @Override
    public short getProtoCode() { return 11002; }

    public Boolean IsSuccess; //是否成功
    public Integer RechargeProductId; //充值产品Id
    public byte RechargeProductType; //充值产品类型
    public Integer Money; //充值后元宝
    public Integer RemainDay; //剩余天数
    public Integer ErrorCode; //错误码

    public byte[] ToArray() {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (DataOutputStream outputStream = new DataOutputStream(out)) {
            dataStream.outputStream = outputStream;
            dataStream.WriteShort(getProtoCode());
            dataStream.WriteBool(IsSuccess);
            if(IsSuccess) {
                dataStream.WriteInt(RechargeProductId);
                dataStream.WriteByte(RechargeProductType);
                dataStream.WriteInt(Money);
                dataStream.WriteInt(RemainDay);
            }
            else {
                dataStream.WriteInt(ErrorCode);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();    }
    public static RoleData_RechargeReturnProto GetProto(byte[] buffer) {
        RoleData_RechargeReturnProto proto = new RoleData_RechargeReturnProto();
        try (DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(buffer))) {
            dataStream.inputStream = inputStream;
            proto.IsSuccess = dataStream.ReadBool();
            if(proto.IsSuccess) {
                proto.RechargeProductId = dataStream.ReadInt();
                proto.RechargeProductType = (byte)dataStream.ReadByte();
                proto.Money = dataStream.ReadInt();
                proto.RemainDay = dataStream.ReadInt();
            }
            else {
                proto.ErrorCode = dataStream.ReadInt();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return proto;
    }
}